package xx.book.controller;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xx.book.util.UploadFileUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("upload")
public class UploadController extends ApiController {

    /**
     * 日志服务
     */
    private static Logger logger = LoggerFactory.getLogger(UploadController.class);

    @Resource
    private UploadFileUtil uploadFileUtil;


    /**
     * 上传文件
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    public R uploadFile(@RequestParam("file") MultipartFile file) {
        return uploadFileUtil.uploadFile(file);
    }

    /**
     * 在线预览
     * @param fileId
     * @param response
     */
    @GetMapping("/previewFile")
    public void previewFile(String fileId, HttpServletResponse response) {
        try {
            uploadFileUtil.filePreview(fileId, response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
